//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.model.wills.DicAdmAreaCode;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.SysUser;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class SysUserDao extends BaseDao<SysUser, String> {
	public SysUserDao() {
	}

	public SysUser chkLogin(String loginName, String password) {
		return (SysUser)this.getByWhere("userCode=:p1 and password=:p2", new Object[]{loginName, password});
	}

	public boolean updateLogin(String userCode, String ipAddr, String device) {
		String hql = "update SysUser set loginTimes=loginTimes+1, lastLogTime=:p1, lastLogIp=:p2, lastLogTe=:p3 where userCode=:p4";
		return this.updateByHql(hql, new Object[]{new Date(), ipAddr, device, userCode}) > 0;
	}

	public boolean updatePassword(String userCode, String newPwd) {
		String hql = "update SysUser set password=:p1, pwdTime=getdate(),updateTime=getdate() where UserCode=:p2";
		return this.updateByHql(hql, new Object[]{newPwd, userCode}) > 0;
	}

	public boolean chkExist(String userCode) {
		return this.getByWhere("userCode=:p1", new Object[]{userCode}) != null;
	}

	public boolean updateExpire(String userCode, int enable) {
		String hql = "update SysUser set expire=:p1 where userCode=:p2";
		return this.updateByHql(hql, new Object[]{Integer.valueOf(enable), userCode}) > 0;
	}

	public boolean updateStatus(String userCode, int status) {
		String hql = "update SysUser set status=:p1 where userCode=:p2";
		return this.updateByHql(hql, new Object[]{Integer.valueOf(status), userCode}) > 0;
	}

	public List<SysUser> getListByGroup(Object... groupCode) {
		ConditionMap cd = new ConditionMap();
		cd.in("groupCode", groupCode);
		ArrayList param = new ArrayList();
		String sqlw = cd.toSql(param);
		return this.queryList("status>0 and " + sqlw, param.toArray(), (new OrderSet()).asc("userCode"), -1);
	}

	public DicShopCode getUserDefalutShop(String userCode) {
		String sql = "select top 1 s.code, s.chName, s.chPinyin, shopType, levelCode, status from DicShopCode s\ninner join SysUserShop a on s.code=a.shopCode\nwhere userCode = :p1 and status=1 order by defaultShop desc, s.code";
		return (DicShopCode)this.getBySql(DicShopCode.class, sql, new Object[]{userCode});
	}

	public boolean chkUserShop(String userCode, String shopCode) {
		String sql = "select count(*) from SysUserShop where userCode=:p1 and shopCode=:p2";
		return ((Integer)this.getBySql(Integer.class, sql, new Object[]{userCode, shopCode})).intValue() > 0;
	}

	public List<DicShopCode> getUserShopList(String userCode, boolean ball) {
		String sql;
		if(ball) {
			sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status, (case when defaultShop is null then 0 else defaultShop end) makeCard from DicShopCode s\nleft join SysUserShop a on s.code=a.shopCode and userCode = :p1\nwhere status>0 order by s.code";
		} else {
			sql = "select s.code, s.chName, defaultShop status from DicShopCode s\ninner join SysUserShop a on s.code=a.shopCode\nwhere userCode = :p1 and status=1 order by s.code";
		}

		return this.queryListBySql(DicShopCode.class, sql, new Object[]{userCode});
	}

	public List<DicAdmAreaCode> getUserAreaList(String userCode, boolean ball) {
		String sql;
		if(ball) {
			sql = "select s.code, s.chName, areaType, (case when a.areaCode is null then 0 else 1 end) status from DicAdmAreaCode s\nleft join SysUserArea a on s.code=a.areaCode and userCode = :p1\nwhere (beginTime is null or beginTime<getdate()) and (endTime is null or endTime>getdate()) order by areaType,s.code";
		} else {
			sql = "select s.code, s.chName, areaType from DicAdmAreaCode s\ninner join SysUserArea a on s.code=a.areaCode\nwhere userCode = :p1 and (beginTime is null or beginTime<getdate()) and (endTime is null or endTime>getdate()) order by areaType,s.code";
		}

		return this.queryListBySql(DicAdmAreaCode.class, sql, new Object[]{userCode});
	}

	public List<DicShopCode> getUserAreaShopList(String userCode) {
		String sql = "select distinct s.code, s.chName, s.chPinyin, shopType, levelCode, s.status, s.sort from DicAdmAreaCode a\ninner join SysUserArea u on a.code=u.areaCode\ninner join DicAdmAreaList l on a.code=l.areaCode\ninner join DicShopCode s on l.shopCode=s.code\nwhere userCode = :p1 and s.status>0 and (beginTime is null or beginTime<getdate()) and (endTime is null or endTime>getdate())\norder by s.sort, s.code";
		return this.queryListBySql(DicShopCode.class, sql, new Object[]{userCode});
	}

	public void updateUserShop(String userCode, Map<String, Integer> shopList) {
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery("delete from SysUserShop where userCode = :p1");
		query.setString("p1", userCode);
		query.executeUpdate();
		query = session.createSQLQuery("insert into SysUserShop (userCode, ShopCode, defaultShop) values (:p1, :p2, :p3)");
		query.setString("p1", userCode);
		Iterator i$ = shopList.keySet().iterator();

		while(i$.hasNext()) {
			String shopCode = (String)i$.next();
			query.setString("p2", shopCode);
			query.setInteger("p3", ((Integer)shopList.get(shopCode)).intValue());
			query.executeUpdate();
		}

	}

	public void updateUserArea(String userCode, List<String> areaList) {
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery("delete from SysUserArea where userCode = :p1");
		query.setString("p1", userCode);
		query.executeUpdate();
		query = session.createSQLQuery("insert into SysUserArea (UserCode, AreaCode) values (:p1, :p2)");
		query.setString("p1", userCode);
		Iterator i$ = areaList.iterator();

		while(i$.hasNext()) {
			String areaCode = (String)i$.next();
			query.setString("p2", areaCode);
			query.executeUpdate();
		}

	}
}
